package edu.scau.hibernate.ui;

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import edu.scau.hibernate.model.User;
import edu.scau.hibernate.service.UserService;
import edu.scau.hibernate.service.UserServiceImpl;

public class MainFrame extends JFrame{
		//用户查询表

		String[] heads={"用户ID","用户名","密码"};
		Object[][] rows=null;
		DefaultTableModel model=new DefaultTableModel(rows,heads);;
		JTable userTable=new JTable(model);;
		JScrollPane scrollPane= new JScrollPane(userTable);
		
		private JPanel p1;
		private JButton alter,delete,update;
		
	public MainFrame() throws Exception{
		
		UserServiceImpl service=new UserServiceImpl();
		List<User> list=service.findAllUser(); 
		Iterator<User> all = list.iterator();
		int len=list.size();//记录表行数

		rows=new Object[len][3];
		len=0;
		while(all.hasNext()){
			User user=all.next(); 
			rows[len][0]=user.getUserId(); 
			rows[len][1]=user.getUserName();
			rows[len][2]=user.getPassword();	

			model.addRow(rows[len]);
			len++;
		}
		
		alter=new JButton("修改");
		delete=new JButton("删除");
		update=new JButton("更新列表");
		p1=new JPanel();
		p1.add(alter);
		p1.add(delete);
		p1.add(update);
		
		setLayout(new BorderLayout(20,15));
		add(new JLabel("可选中一行修改或删除！"),BorderLayout.NORTH);
		add(scrollPane);
		add(p1,BorderLayout.SOUTH);
		
		setTitle("全部用户信息");
		setSize(500,400);
		setLocation(500,250);
		
		alter.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int x;
				int n=-1;
				x=userTable.getSelectedRowCount();//记录选中行总数

				if(x!=1)//控制是否选中表格一行

					JOptionPane.showMessageDialog(null, "请选中表格某一行！","Message",JOptionPane.ERROR_MESSAGE);
				
				else{
					n=userTable.getSelectedRow();//记录选择行数

					if(n>=0){
							try {
								User user=new User();
								user.setUserId((int)rows[n][0]);
								user.setUserName((String)rows[n][1]);
								
								UpdateUser alt=new UpdateUser();
								alt.setUser(user);
								alt.setVisible(true);
								alt.name.setText(user.getUserName());
							} catch (Exception e1) {
								// TODO Auto-generated catch block

								e1.printStackTrace();
							}	
					}
				}		
			}
		});
		
		
		delete.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				
				int x;
				int n=-1;
				x=userTable.getSelectedRowCount();//记录选中行总数

				if(x!=1)//控制是否选中表格一行

					JOptionPane.showMessageDialog(null, "请选中表格某一行！","Message",JOptionPane.ERROR_MESSAGE);
				
				else{
					n=userTable.getSelectedRow();//记录选择行数

					if(n>=0){
						String name =(String)rows[n][0];
						int option =JOptionPane.YES_OPTION;
						option=JOptionPane.showConfirmDialog(null, "确认删除用户?   "+name);
						
						if(option==JOptionPane.YES_OPTION){
							try {
								UserService service=new UserServiceImpl();
								if(service.deleteUser(service.findUserByUserName(name)))
									JOptionPane.showMessageDialog(null, "删除成功！");
							} catch (Exception e1) {
								// TODO Auto-generated catch block

								e1.printStackTrace();
							}
						}	
					}
				}
			}	
		});
		
		update.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				MainFrame frame;
				try {
					frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e1) {
					// TODO Auto-generated catch block

					e1.printStackTrace();
				}
			}
		});
	}
	
/*	public static void main(String[] args) throws Exception {
		MainFrame frame=new MainFrame();
		frame.setVisible(true);
	}
	
	**/
}